package com.example.dao;

import com.example.model.FileInfo;
import com.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FileInfoDaoImpl implements FileInfoDao {
    @Override
    public int insertFileInfo(FileInfo fileInfo) {
        Connection connection = DBUtil.getConnection();
        int count = 0;
        String sql = "insert into file_info(uuid,file_size,file_type,file_origin_name,file_create_time,file_path,file_signature) values(?,?,?,?,?,?,?)";
        try {
            PreparedStatement pre = connection.prepareStatement(sql);
            pre.setString(1, fileInfo.getUuid());
            pre.setDouble(2, fileInfo.getFileSize());
            pre.setString(3, fileInfo.getFileType());
            pre.setString(4, fileInfo.getFileOriginName());
            pre.setString(5, fileInfo.getFileCreateTime());
            pre.setString(6, fileInfo.getFilePath());
            pre.setString(7, fileInfo.getFileSignature());
            count = pre.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<FileInfo> listFileInfos() {
        Connection connection = DBUtil.getConnection();
//        String url = "select * from file_info ORDER BY file_create_time DESC limit 10";
        // derby 用不了limit 10
        String url = "select * from file_info ORDER BY file_create_time DESC";
        List<FileInfo> list = new ArrayList<>();
        try {
            PreparedStatement pre = connection.prepareStatement(url);
            ResultSet resultSet = pre.executeQuery();
            while (resultSet.next()) {
                String uuid = resultSet.getString("uuid");
                double fileSize = resultSet.getDouble("file_size");
                String fileType = resultSet.getString("file_type");
                String file_origin_name = resultSet.getString("file_origin_name");
                String fileCreateTime = resultSet.getString("file_create_time");
                String filePath = resultSet.getString("file_path");
                String fileSignature = resultSet.getString("file_signature");
                FileInfo fileInfo = new FileInfo();
                fileInfo.setUuid(uuid);
                fileInfo.setFileSize(fileSize);
                fileInfo.setFileType(fileType);
                fileInfo.setFileOriginName(file_origin_name);
                fileInfo.setFileCreateTime(fileCreateTime);
                fileInfo.setFilePath(filePath);
                fileInfo.setFileSignature(fileSignature);
                list.add(fileInfo);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public FileInfo findByUUid(String UUid) {
        Connection connection = DBUtil.getConnection();
        String url = "select * from file_info where uuid = ?";
        FileInfo fileInfo = new FileInfo();
        try {
            PreparedStatement pre = connection.prepareStatement(url);
            pre.setString(1, UUid);
            ResultSet resultSet = pre.executeQuery();
            if (resultSet.next()) {
                String uuid = resultSet.getString("uuid");
                double fileSize = resultSet.getDouble("file_size");
                String fileType = resultSet.getString("file_type");
                String file_origin_name = resultSet.getString("file_origin_name");
                String fileCreateTime = resultSet.getString("file_create_time");
                String filePath = resultSet.getString("file_path");
                String fileSignature = resultSet.getString("file_signature");
                fileInfo.setUuid(uuid);
                fileInfo.setFileSize(fileSize);
                fileInfo.setFileType(fileType);
                fileInfo.setFileOriginName(file_origin_name);
                fileInfo.setFileCreateTime(fileCreateTime);
                fileInfo.setFilePath(filePath);
                fileInfo.setFileSignature(fileSignature);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fileInfo;
    }
}
